package com.item.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.*;

import com.base.util.DateUtil;


public class MyDateUtils {
	
	public static String dateAdd(Date date, int gap)throws Exception{
		return parseDateWithDay(DateUtil.add(date, gap));
	}
	
	
	public static String dateStringAdd(String date, int gap)throws Exception{
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Date d = sdf.parse(date);
		return parseDateWithDay(DateUtil.add(d, gap));
	}
	
    public static String parseDateWithDay(Date date){
    	return DateUtil.dateToStr(date, "yyyy-MM-dd");
    }
    
    public static int dayDiff(String date1, String date2)throws Exception{
    	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
    	return dayDiff(sdf.parse(date1), sdf.parse(date2));
    }
    
    public static int dayDiff(Date date1, Date date2) {
        Calendar cal = Calendar.getInstance();    
        cal.setTime(date1);    
        long time1 = cal.getTimeInMillis();                 
        cal.setTime(date2);    
        long time2 = cal.getTimeInMillis();         
        long between_days=(time2-time1)/(1000*3600*24);  
        int res = Integer.parseInt(String.valueOf(between_days));
        return res < 0 ? 0 : res;
    }
}
